package com.cd.socialMarketing.service.generic;

/** @author Agustin Cingolani
 *  02/05/2013
 */

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Criterion;

import com.cd.socialMarketing.model.AbstractEntityLong;
import com.cd.socialMarketing.repository.HibernateObjectDao;

public class SimpleManager<K extends Serializable, E extends AbstractEntityLong> implements IGenericManager<K, E>{

    protected Class<E> type;
    private HibernateObjectDao<K, E> dao;
    
    public SimpleManager(Class<E> _type) {
        type = _type;
    }   

	public HibernateObjectDao<K, E> getDao() {
        if(!dao.getPersistentClass().equals(type)) 
            System.out.println("DAO:" + dao.getPersistentClass().getName() + " - MAN: " + type.getName());

        return dao;
    }

    public void setDao(HibernateObjectDao<K, E> dao) {
        if(dao.getPersistentClass().equals(type)) 
            this.dao = dao;
        else 
            throw new RuntimeException("El tipo del DAO: " + dao.getPersistentClass().getName()
                    + " no coincide con el del Manager: " + type.getName());
    }
    
    public E get(K id) {
        return getDao().get(id);
    }

    public List<E> list() {
        return getDao().list();
    }
    
    @Override
    public List<E> list(String order, String type) {
    	return getDao().list(order, type);
    }

    @Override
    public List<E> list(Integer from, Integer count) {
    	return getDao().list(from, count);
    }
    
    @Override
    public List<E> list(Integer from, Integer count, String order, String type) {
    	return getDao().list(from, count, order, type);
    }
    
    public List<E> findByName(String nombre) {
        return getDao().findByName(nombre);
    }

    public Map<String, Object> filtro(Object... keyValue) {
        Map<String, Object> filtro = new HashMap<String, Object>();
        for (int i = 0; i < keyValue.length; i+=2) {
            filtro.put((String) keyValue[i], keyValue[i+1]);
        }
        return filtro;
    }

    public List<E> find(Map<String, Object> filtro) {
        return getDao().find(filtro);
    }

    @Override
    public List<E> find(Map<String, Object> filtro, Integer from, Integer count) {
    	return getDao().find(filtro, from, count);
    }
    
    @Override
    public List<E> find(Map<String, Object> filtro, Integer from, Integer count, String order, String type) {
    	return getDao().find(filtro, from, count, order, type);
    }
    
    @Override
	public List<E> find(Criterion criterion, Integer from, Integer count) {
		return getDao().find(criterion, from, count);
	}
    
    @Override
	public List<E> find(Criterion criterion, Integer from, Integer count, String order, String type) {
		return getDao().find(criterion, from, count, order, type);
	}
    
    public E findUnique(Map<String, Object> filtro) {
        return getDao().findUnique(filtro);
    }
    
    @Override
	public Long count(Map<String, Object> filtro) {
		return getDao().count(filtro);
	}
    
    @Override
	public Long count(Criterion criterion) {
    	return getDao().count(criterion);
	}

    public void save(E entity) {
    	getDao().save(entity);
    }
    
    public void delete(E entity) {
    	getDao().delete(entity);
    }

}
